<?php
/**
 * Created by PhpStorm.
 * User: Lee
 * Date: 2021/7/1
 */
namespace App\Services\Backend\System;

use App\Common\Exception\ErrorException;
use App\Models\Mobile\MobileStudent;
use Illuminate\Support\Facades\DB;

/**
 * 学生 (服务)
 */
class StudentService
{
    /** @var \App\Repositories\Backend\System\StudentRepository $studentRepository */
    private $studentRepository;

    public function __construct()
    {
        $this->studentRepository = app('studentRepository');
    }

    // 获取列表
    public function getList($requestData){
        $qrySql = "SELECT t.*,s.mobile AS bindmobile ";
        $qrySql .= "FROM students AS t LEFT JOIN mobile_student AS s ON t.cid = s.cid ";
        $qrySql .= "WHERE 1=1";

        // 搜索
        if(isset($requestData['filters']['name']) && !empty($requestData['filters']['name'])){
            $qrySql .= " AND t.name like '%{$requestData['filters']['name']}%'";
        }
        if(isset($requestData['filters']['cid']) && !empty($requestData['filters']['cid'])){
            $qrySql .= " AND t.cid like '%{$requestData['filters']['cid']}%'";
        }
        if(isset($requestData['filters']['mobile']) && !empty($requestData['filters']['mobile'])){
            $qrySql .= " AND s.mobile like '%{$requestData['filters']['mobile']}%'";
        }

        $result = DB::table(DB::raw("($qrySql) as studentlist"))->orderBy('cid','asc')->paginate($requestData['limit'],['*'],'page',$requestData['page']);

        return $result->toArray();
    }

    //编辑
    public function edit($requestData){
        $o_cid = $requestData['o_cid'];
        $bindmobile = $requestData['bindmobile'];
        unset($requestData['o_cid']);
        unset($requestData['bindmobile']);

        $student = $this->studentRepository->get($requestData['cid'],'cid');
        if($student){
            if($student->cid != $o_cid){
                throw new ErrorException('证件号码为'.$requestData['cid'].'的学生已存在');
            }
        }

        $this->studentRepository->edit($requestData, $o_cid, 'cid');

        $mobileStudent = MobileStudent::where('mobile', $bindmobile)->first();
        if($mobileStudent){
            if($mobileStudent->cid != $o_cid){
                throw new ErrorException('手机号码'.$bindmobile.'已被绑定');
            }
        }

        /** @var \App\Services\Mobile\MobileStudentService $mobileStudentService */
        $mobileStudentService = app('mobileStudentService');
        $mobileStudentService->deleteCore(['cid'=>$o_cid],FALSE);
        $mobileStudentService->addCore(['mobile'=>$bindmobile,'cid'=>$requestData['cid']]);
    }

    //删除
    public function delete($requestData){
        $cid = $requestData['cid'];
        $studentRow = $this->studentRepository->get($cid,'cid');
        if(!$studentRow){
            throw new ErrorException('学生信息不存在');
        }

        $this->studentRepository->delete($cid,'cid');
        /** @var \App\Services\Mobile\MobileStudentService $mobileStudentService */
        $mobileStudentService = app('mobileStudentService');
        $mobileStudentService->deleteCore(['cid'=>$cid],FALSE);
    }

    //*******************外部调用方法****************************************************
    public function getCore($id, $keyName = 'cid',$columns = ['*']){
        return $this->studentRepository->get($id,$keyName,$columns);
    }

    public function getAllCore($filters=[], $sorts =[], $columns=['*'],$whereRaw = FALSE){
        return $this->studentRepository->getAll($filters,$sorts,$columns,$whereRaw);
    }

    public function addCore($data){
        return $this->studentRepository->add($data);
    }

    public function editCore($data,$id,$keyName='cid')
    {
        return $this->studentRepository->edit($data,$id,$keyName);
    }

}
